﻿using System;
using System.Collections.Generic;
using Sinacor.Infra.UI.Common;
using Sinacor.Infra.UI.Security.Services;
using Sinacor.UI.Bovespa.Ordens.Communication.Bovespa.Ordens.AjusteMercadoFuturo;

namespace Sinacor.UI.Bovespa.Ordens.Communication
{
    /// <summary>
    /// Classe Communication para o cenário de Cadastro de Ajustes Mercado Futuro
    /// </summary>
    public class AjusteMercadoFuturoCommunication : BaseCommunication
    {
        #region Construtor

        public AjusteMercadoFuturoCommunication(SessionInformation session) : base(session) { }

        #endregion

        #region Métodos

        /// <summary>
        /// Calcula ajuste de posição de mercado futuro
        /// </summary>
        /// <param name="request">Data do Pregão</param>
        public void AjustarPosicao(DateTime request)
        {
            try
            {
                DataPregaoRequest dataPregao = new DataPregaoRequest();
                dataPregao.Valor = request;

                using (ServiceInstance<IAjusteMercadoFuturo> serviceManager = new ServiceInstance<IAjusteMercadoFuturo>())
                {
                    IAjusteMercadoFuturo servico = serviceManager.TryGetInstance(this.Session);
                    servico.AjustarPosicao(dataPregao);
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }
        }

        /// <summary>
        /// Altera um registro de ajuste de mercado futuro
        /// </summary>
        /// <param name="request">Model AjusteMercadoFuturo</param>
        public void AlterarMercadoFuturo(Model.AjusteMercadoFuturo request)
        {
            try
            {
                using (ServiceInstance<IAjusteMercadoFuturo> serviceManager = new ServiceInstance<IAjusteMercadoFuturo>())
                {
                    IAjusteMercadoFuturo servico = serviceManager.TryGetInstance(this.Session);
                    servico.AlterarAjusteMercadoFuturo(Translator.ModelToDataContract.AjusteMercadoFuturo.ToMercadoFuturoRequest(request));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }
        }

        /// <summary>
        /// Consulta registros de ajustes de mercado futuro
        /// </summary>
        /// <param name="request">Model.Parameter FiltroAjusteMercadoFuturo</param>
        /// <returns>ModelCollection de Model.AjusteMercadoFuturo</returns>
        public ModelCollection<Model.AjusteMercadoFuturo> ConsultarMercadoFuturo(Model.Parameter.FiltroAjusteMercadoFuturo request)
        {
            try
            {
                using (ServiceInstance<IAjusteMercadoFuturo> serviceManager = new ServiceInstance<IAjusteMercadoFuturo>())
                {
                    IAjusteMercadoFuturo servico = serviceManager.TryGetInstance(this.Session);
                    return Translator.DataContractToModel.AjusteMercadoFuturo.ToAjusteMercadoFuturo(servico.ConsultarAjustesMercadoFuturo(Translator.ModelToDataContract.AjusteMercadoFuturo.ToFiltroMercadoFuturoRequest(request)));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
                return null;
            }
        }

        /// <summary>
        /// Exclui um registro de ajuste de mercado futuro
        /// </summary>
        /// <param name="request">Model.Parameter FiltroAjusteMercadoFuturo</param>
        public void ExcluirMercadoFuturo(Model.Parameter.FiltroAjusteMercadoFuturo request)
        {
            try
            {
                using (ServiceInstance<IAjusteMercadoFuturo> serviceManager = new ServiceInstance<IAjusteMercadoFuturo>())
                {
                    IAjusteMercadoFuturo servico = serviceManager.TryGetInstance(this.Session);
                    servico.ExcluirAjusteMercadoFuturo(Translator.ModelToDataContract.AjusteMercadoFuturo.ToFiltroMercadoFuturoRequest(request));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }
        }

        /// <summary>
        /// Executa as transações de Inserção, Exclusão e Alteração de registros de ajuste de mercado futuro
        /// </summary>
        /// <param name="listaInclusao">ModelCollection de Model.AjusteMercadoFuturo</param>
        /// <param name="listaAlteracao">ModelCollection de Model.AjusteMercadoFuturo</param>
        /// <param name="listaExclusao">ModelCollection de Model.Parameter.FiltroAjusteMercadoFuturo</param>
        public void ExecutarTransacoes(ModelCollection<Model.AjusteMercadoFuturo> listaInclusao, ModelCollection<Model.AjusteMercadoFuturo> listaAlteracao, ModelCollection<Model.Parameter.FiltroAjusteMercadoFuturo> listaExclusao)
        {
            try
            {
                using (ServiceInstance<IAjusteMercadoFuturo> serviceManager = new ServiceInstance<IAjusteMercadoFuturo>())
                {
                    IAjusteMercadoFuturo servico = serviceManager.TryGetInstance(this.Session);
                    servico.ExecutarTransacoes(Translator.ModelToDataContract.AjusteMercadoFuturo.ToMercadoFuturoRequest(listaInclusao),
                                                                Translator.ModelToDataContract.AjusteMercadoFuturo.ToMercadoFuturoRequest(listaAlteracao),
                                                                Translator.ModelToDataContract.AjusteMercadoFuturo.ToFiltroMercadoFuturoRequest(listaExclusao));                    
                }


                
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }
        }

        /// <summary>
        /// Inclui um registro de ajuste de mercado futuro
        /// </summary>
        /// <param name="request">Model AjusteMercadoFuturo</param>
        public void InserirMercadoFuturo(Model.AjusteMercadoFuturo request)
        {
            try
            {
                using (ServiceInstance<IAjusteMercadoFuturo> serviceManager = new ServiceInstance<IAjusteMercadoFuturo>())
                {
                    IAjusteMercadoFuturo servico = serviceManager.TryGetInstance(this.Session);
                    servico.IncluirAjusteMercadoFuturo(Translator.ModelToDataContract.AjusteMercadoFuturo.ToMercadoFuturoRequest(request));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }
        }

        #endregion
    }
}